package com.yyk.yiliao.ui.trade;

import androidx.appcompat.app.AlertDialog;
import androidx.cardview.widget.CardView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.util.DialogUtil;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.glide.GlideUtils;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.UserbankUntyingInfo;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的 银行卡
 */
public class MyBankcardXQActivity extends BaseActivity {
    public static MyBankcardXQActivity intanse;

    @BindView(R.id.bt_next)
    Button btNext;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_cardtype_name)
    TextView tvCardtypeName;
    @BindView(R.id.tv_numberu)
    TextView tvNumberu;
    @BindView(R.id.cv_cardview)
    CardView cvCardview;
    private String id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_bankcard_xq;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("我的银行卡");
    }

    @Override
    public void initData() {
        intanse = this;
        initGetIntent();
    }

    private void initGetIntent() {
        if (getBundle() != null) {
            id = getBundle().getString("id");
            String cardtype = getBundle().getString("cardtype");
            String img = getBundle().getString("img");
            String name = getBundle().getString("name");
            String numberu = getBundle().getString("numberu");


            if (cardtype.equals("1")) {
                tvCardtypeName.setText(name + " 信用卡");
            } else {
                tvCardtypeName.setText(name + " 储蓄卡");
            }
            GlideUtils.load(mActivity, ApiService.BASE_URL + img, ivImg);
            tvNumberu.setText(numberu);

        }
    }

    @OnClick(R.id.bt_next)
    public void onViewClicked() {
        DialogUtil.getIntanse().simpleDialog(mActivity, "", "解绑后银行服务将不可用", "确认", "取消", new DialogUtil.AlertDialogListenter() {
            @Override
            public void queding() {
                DialogUtil.getIntanse().showPayDialog(mActivity, new DialogUtil.PayListenter() {
                    @Override
                    public void onClick(String password, AlertDialog dialog, TextView textView) {
                        if (password.length() == 6) {
                            Map<String, String> map = new TreeMap<>();
                            map.put("id", Hawk.get("uid") + "");
                            map.put("cardid", id + "");
                            map.put("pwd", password + "");
                            map.put("sign", Md5Util2.createSign(map));
                            RxUtils.getInstance().retrofit.create(ApiService.class).postUserbankUntying(map)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .unsubscribeOn(Schedulers.io())
                                    .subscribe(new Subscriber<UserbankUntyingInfo>() {
                                        @Override
                                        public void onCompleted() {

                                        }

                                        @Override
                                        public void onError(Throwable e) {

                                        }

                                        @Override
                                        public void onNext(UserbankUntyingInfo info) {
                                            Logger.e("患者解绑银行卡" + info.toString());
                                            if (info.getCode() == 1) {
                                                startAct(UnbindCardSuccendActivity.class, true);
                                                textView.setVisibility(View.GONE);
                                                dialog.dismiss();
                                                return;
                                            } else if (info.getCode() == 2) {
                                                textView.setVisibility(View.VISIBLE);
                                            } else {
//                                                ToastUtil.showShort(mActivity, info.getMsg());
                                                textView.setVisibility(View.GONE);
                                            }
                                        }
                                    });
                        } else {
                            textView.setVisibility(View.GONE);
                        }
                    }
                });
            }

            @Override
            public void quxiao() {

            }
        }).show();
    }


}
